package com.blackbird.jang.model;

import com.blackbird.jang.MainActivity;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.utils.TargetSelector;

/**
 * 道具
 * 
 * @author jang
 * 
 */
public class ToolButton extends Button {

	private boolean isSelected = false;

	public ToolButton(Node normalSprite, Node selectedSprite,
			Node disabledSprite, Node focusedSprite,
			TargetSelector downSelector, TargetSelector upSelector) {
		super(normalSprite, selectedSprite, disabledSprite, focusedSprite,
				downSelector, upSelector);
		setScale(MainActivity.SCALE_X, MainActivity.SCALE_Y);
	}

	public ToolButton(Node normalSprite, Node selectedSprite,
			Node disabledSprite, Node focusedSprite, TargetSelector selector) {
		super(normalSprite, selectedSprite, disabledSprite, focusedSprite,
				selector);
		setScale(MainActivity.SCALE_X, MainActivity.SCALE_Y);
	}

	public static ToolButton make(int normalResId, int selectedResId,
			int disabledResId, int focusedResId, Node target, String selector) {

		return new ToolButton(
				Sprite.make(normalResId),
				selectedResId != 0 ? ((Node) (Sprite.make(selectedResId)))
						: null,
				disabledResId != 0 ? ((Node) (Sprite.make(disabledResId)))
						: null,
				focusedResId != 0 ? ((Node) (Sprite.make(focusedResId))) : null,
				new TargetSelector(target, selector, null));
	}

	/**
	 * 闪烁
	 */
	public void sparkle() {

		IntervalAction a = (IntervalAction) ScaleTo.make(0.5f, 0.5f, 0.67f)
				.autoRelease();
		IntervalAction r = (IntervalAction) a.reverse().autoRelease();
		IntervalAction seq = (IntervalAction) Sequence.make(a, r).autoRelease();
		Action sparkle = (Action) RepeatForever.make(seq).autoRelease();

		this.runAction(sparkle);
	}

	/**
	 * 恢复初始状态
	 */
	public void reset() {
		isSelected = false;
		stopAllActions();
		setScale(MainActivity.SCALE_X, MainActivity.SCALE_Y);
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}
	
}
